package com.example.blog_system.controller;

import com.example.blog_system.service.ArticleService;
import com.example.blog_system.service.TagService;
import com.example.blog_system.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ViewController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("articles", 
            articleService.getAllArticles(
                PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "createdAt"))
            ).getContent());
        model.addAttribute("tags",
            tagService.getAllTags(
                PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "name"))
            ).getContent());
        return "index";
    }

    @GetMapping("/login")
    public String login(Authentication authentication) {
        if (authentication != null && authentication.isAuthenticated() && 
            !"anonymousUser".equals(authentication.getPrincipal())) {
            return "redirect:/";
        }
        return "login";
    }

    @GetMapping("/register")
    public String register(Authentication authentication) {
        if (authentication != null && authentication.isAuthenticated() && 
            !"anonymousUser".equals(authentication.getPrincipal())) {
            return "redirect:/";
        }
        return "register";
    }

    @GetMapping("/article/{id}")
    public String article(@PathVariable Long id, Model model, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
        try {
            var article = articleService.getArticleById(id);
            if (article == null) {
                model.addAttribute("error", "文章不存在");
                return "redirect:/";
            }
            
            model.addAttribute("article", article);
            var commentsPage = commentService.getCommentsByArticle(id, PageRequest.of(page - 1, size, Sort.by(Sort.Direction.DESC, "createdAt")));
            model.addAttribute("comments", commentsPage.getContent());
            model.addAttribute("commentsPage", commentsPage);
            model.addAttribute("currentPage", page);
            model.addAttribute("pageSize", size);
            return "article";
        } catch (Exception e) {
            model.addAttribute("error", "加载文章失败：" + e.getMessage());
            return "redirect:/";
        }
    }

    @GetMapping("/write")
    public String writeArticle(Authentication authentication) {
//        if (authentication == null || !authentication.isAuthenticated() ||
//            "anonymousUser".equals(authentication.getPrincipal())) {
//            return "redirect:/login";
//        }
        return "write";
    }

    @GetMapping("/profile")
    public String profile(Authentication authentication) {
        if (authentication == null || !authentication.isAuthenticated() || 
            "anonymousUser".equals(authentication.getPrincipal())) {
            return "redirect:/login";
        }
        return "profile";
    }
} 